package DAO;

import Connection.ConnectionFactory;
import Interfaces.Inter_Questionario;
import Model.Questionario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class DAO_Questionario implements Inter_Questionario{
     
    Connection con;
     
     public DAO_Questionario() throws Exception{
        try{
            this.con =ConnectionFactory.getConnection();
        }
        catch(Exception e){
            throw new Exception("Erro ao conectar no Questionario\n"+e.getMessage());
        }
    }
     
      public void GravaQuest(Questionario quest) throws Exception{
        PreparedStatement ps;
        ResultSet rs;

        if(quest ==null){
            throw new Exception("Nao pode gravar um questionario nulo");
        }

        try{
            String sql ="INSERT INTO QUESTIONARIO (idQuestionario,resp1,resp2,resp3,resp4,resp5,resp6,resp7,resp8,res) values(?,?,?,?,?,?,?,?,?,?)";
            ps =con.prepareStatement(sql);
            ps.setInt(1,quest.getIdQuestionario());            
            ps.setInt(2,quest.getResp1());
            ps.setInt(3,quest.getResp2());
            ps.setInt(4,quest.getResp3());
            ps.setInt(5,quest.getResp4());
            ps.setInt(6,quest.getResp5());
            ps.setInt(7,quest.getResp6());
            ps.setInt(8,quest.getResp7());
            ps.setInt(9,quest.getResp8());
            ps.setInt(10,quest.getRes());
            
            ps.executeUpdate();
        }
         catch(SQLException e)
        {
            throw new Exception("Erro ao inserir QUESTIONARIO \n"+e.getMessage());
        }
    }
    
     public List ListaQuest() throws Exception{
        PreparedStatement ps;
        ResultSet rs;
        try{
            ps =con.prepareStatement("select * from questionario order by idQuestionario asc");
            rs =ps.executeQuery();

            List<Questionario> list = new ArrayList<Questionario>();

            while(rs.next()){
                int idQuestionario =rs.getInt(1);
                int resp1 =rs.getInt(2);
                int resp2 =rs.getInt(3);
                int resp3 =rs.getInt(4);
                int resp4 =rs.getInt(5);
                int resp5 =rs.getInt(6);
                int resp6 =rs.getInt(7);
                int resp7 =rs.getInt(8);
                int resp8 =rs.getInt(9);
                int res =rs.getInt(10);
                

                Questionario quest;
                quest= new Questionario(idQuestionario, resp1, resp2, resp3, resp4, resp5, resp6, resp7, resp8, res);
                
                list.add(quest);
            }
            return list;
        }
        catch(SQLException e){
            throw new Exception("Erro ao listar USUÁRIOS \n"+e.getMessage());
        }
    }
     
      public Questionario LocalizaQuest(int idQuestionario) throws Exception{
        PreparedStatement ps;
        ResultSet rs;

        if(idQuestionario == 0){
            throw new Exception("Não pode Localizar QUESTIONARIO de ID igual a 0");
        }
        try{
            ps =con.prepareStatement("select * from Questionario where idQuestionario = ?");
            ps.setInt(1, idQuestionario);
            rs =ps.executeQuery();

            if(!rs.next()){
                throw new Exception("Não foi encontrado nenhum QUESTIONARIO com o ID "+idQuestionario);
            }
                int resp1 =rs.getInt(2);
                int resp2 =rs.getInt(3);
                int resp3 =rs.getInt(4);
                int resp4 =rs.getInt(5);
                int resp5 =rs.getInt(6);
                int resp6 =rs.getInt(7);
                int resp7 =rs.getInt(8);
                int resp8 =rs.getInt(9);
                int res =rs.getInt(10);

             Questionario quest;
             quest= new Questionario(idQuestionario, resp1, resp2, resp3, resp4, resp5, resp6, resp7, resp8, res);
             return quest;
        }catch(SQLException e){
            throw new Exception("Erro ao localizar administrador \n"+e.getMessage());
        }
    }
    
};
